package com.huiminpay.merchant.controller;

import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.merchant.common.SecurityUtil;
import com.huiminpay.merchant.convert.AppVoConvert;
import com.huiminpay.merchant.vo.AppVo;
import com.yh.merchant.api.Service.IAppService;
import com.yh.merchant.api.dto.AppDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Api(value = "商户平台-应用管理", tags = "商户平台-应用相关", description = "商户平台-应用相关")
public class AppController {
    @Reference
    IAppService appService;

    @ApiOperation("商户创建应用")
    //                  接收参数名称       参数的意义          是否必填
//    @ApiImplicitParam(name = "appVo",value = "应用信息",required = true)
    @PostMapping("/my/apps")
    public Object creatApp(@RequestBody AppVo appVo){
        //1.非空
        if(StringUtils.isEmpty(appVo.getAppName())){
            throw new BusinessException(CommonErrorCode.E_200001);
        }
        //2.对象转换
        AppDTO appDTO = AppVoConvert.INSTANCE.vo2Dto(appVo);
        //3.保存数据（获取当前登录商户id）
        Long merchantId = SecurityUtil.getMerchantId();
        appDTO.setMerchantId(merchantId);
        return appService.crratApp(appDTO);
    }

    @ApiOperation("根据id查询应用详细信息(appId)")
    @ApiImplicitParam(name = "appVo",value = "应用id",required = true)
    @PostMapping("my/app/{appId}")
    public Object myApp(@PathVariable String appId){
        AppDTO appDTO = appService.myapp(appId);
        return AppVoConvert.INSTANCE.dto2Vo(appDTO);
    }

    @ApiOperation("商户查询自己的所有应用")

    @GetMapping("/my/apps")
    public Object myApps(){

        Long merchantId = SecurityUtil.getMerchantId();
        List<AppDTO> appDTOS =appService.myApps(merchantId);
        return AppVoConvert.INSTANCE.listDto2ListVo(appDTOS);
    }
}
